<?php

namespace App\Http\Controllers\Innerend;

use App\Models\Admin;
use App\Models\Article;
use App\Models\ArticleAddon;
use App\Models\SiteConfig;
use App\Models\SiteInfo;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class SiteInfoController extends BaseController
{

    // 话题
    public function initialize()
    {
        parent::initialize();

        $key = $this->getData('key', '', 'trim');
        $url = url('/innerend/site-info/content?key='.$key);

        $this->assign('init_url', $url);
        $this->model = 'SiteInfo';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作


    // 展示内容
    public function content()
    {
        $key = $this->getData('key', null, 'trim');
        $this->throwIf(empty($key), '请选择你要操作的数据！');

        $model = new SiteInfo();
        $info  = $model->isCache(true)->oneFind(['key' => $key]);
        $form  = $this->adminForm();

        $idHtml  = $form->formHidden('id', $info['id'] ?? 0);
        $keyHtml = $form->formHidden('key', $info['key'] ?? '');
        $title   = $form->formInput('title', $info['title'] ?? '', '标题');
        $content = $form->formUeditor('content', $info['content'] ?? '');
        //$content = $form->formTextarea('content', $info['content'] ?? '');


        $formHtml = $idHtml;
        $formHtml .= $keyHtml;
        $formHtml .= $form->makeItem('标题', $title);
        $formHtml .= $form->makeItem('内容', $content);

        return $this->dispaly('form', ['formHtml' => $formHtml]);
    }


    // 添加
    protected function _before_add()
    {
        $data = [
            'title'   => $this->getData('title', null, 'trim'),
            'content' => $this->getData('content', null, 'trim'),
            'status'  => $this->getData('status', 1, 'intval'),
        ];

        // 文章内容处理
        if($data['content']){
            $data['content'] = htmlspecialchars_decode($data['content']);
            $data['content'] = str_replace("<br/>", "", $data['content']);
            //$data['content'] = str_replace("<p><br/></p>", "", $data['content']);
        }

        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $data = [
            'title'   => $this->getData('title', null, 'trim'),
            'content' => $this->getData('content', null, 'trim'),
            'status'  => $this->getData('status', 1, 'intval'),
        ];

        // 文章内容处理
        if($data['content']){
            $data['content'] = htmlspecialchars_decode($data['content']);
            $data['content'] = str_replace("<br/>", "", $data['content']);
            //$data['content'] = str_replace("<p><br/></p>", "", $data['content']);
        }

        return $data;
    }

}


